package com.xyht.sca_s.student_manage_system.modules.appDynamic.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.TimeUtil;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.entity.*;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.entity.req.*;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.entity.resp.*;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.mapper.*;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.service.AppControlOrderService;
import com.xyht.sca_s.student_manage_system.modules.meeting.entity.req.CheckboxReq;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.appDynamic.constant.ControlTypeConstant.*;
import static com.xyht.sca_s.student_manage_system.modules.appDynamic.constant.NodeTypeConstant.CONDITION_NODE;

/**
 * <p>
 * 控件顺序表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-07-26
 */
@Service
public class AppControlOrderServiceImpl extends ServiceImpl<AppControlOrderMapper, AppControlOrder> implements AppControlOrderService {
    @Resource
    private AppControlOrderMapper appControlOrderMapper;
    @Resource
    private AppTextControlConfigMapper appTextControlConfigMapper;
    @Resource
    private AppTimeControlConfigMapper appTimeControlConfigMapper;
    @Resource
    private AppFileControlConfigMapper appFileControlConfigMapper;
    @Resource
    private AppOptionControlConfigMapper appOptionControlConfigMapper;
    @Resource
    private AppStepperControlConfigMapper appStepperControlConfigMapper;
    @Resource
    private AppBasicInfoMapper appBasicInfoMapper;
    @Resource
    private AppProcessNodeConfigMapper appProcessNodeConfigMapper;
    @Resource
    private AppConditionGroupMapper appConditionGroupMapper;
    @Resource
    private AppConditionMapper appConditionMapper;

    @Override
    @Transactional
    public ResponseResult saveFormDesign(FormDesignSaveReq formSaveReq) {
        //校验appId
        String appId = formSaveReq.getAppId();
        if (isNullOrEmpty(appId)) {
            return CommonResult.failed(CommonCodeEnum.DYNAMIC_APP_NOT_EXIST);
        }
        //校验应用是否存在
        AppBasicInfo appBasicInfo = appBasicInfoMapper.selectById(appId);
        if (isNullOrEmpty(appBasicInfo) || appBasicInfo.getId() == null) {
            return CommonResult.failed(CommonCodeEnum.DYNAMIC_APP_NOT_EXIST);
        }
        //校验当前应用是否已存在控件
        List<AppControlOrder> appControlOrderList = appControlOrderMapper.selectList(new LambdaQueryWrapper<AppControlOrder>()
                .eq(AppControlOrder::getAppId, appId));
        if (!appControlOrderList.isEmpty()) {
            return CommonResult.failed(CommonCodeEnum.CONTROL_ALREADY_ADD);
        }
        //校验是否传入控件信息
        if (formSaveReq.getControlOrderList().size() == 0) {
            return CommonResult.failed(CommonCodeEnum.CONTROL_NOT_ADD);
        }
        //校验控件是否完全设置
        if (formSaveReq.getControlOrderList().size() != formSaveReq.getTextControlConfigList().size() + formSaveReq.getTimeControlConfigList().size() + formSaveReq.getFileControlConfigList().size() + formSaveReq.getOptionControlConfigList().size() + formSaveReq.getStepperControlConfigList().size()) {
            return CommonResult.failed(CommonCodeEnum.CONTROL_NOT_SETTING_ENOUGH);
        }

        //保存数据
        for (AppControlOrderReq controlOrderReq : formSaveReq.getControlOrderList()) {
            if (isNullOrEmpty(controlOrderReq.getType(), controlOrderReq.getControlUniqueId(), controlOrderReq.getSort(), controlOrderReq.getIsShow())) {
                return CommonResult.failed(CommonCodeEnum.CONTROL_ORDER_NOT_SETTING_ENOUGH);
            }
            //保存控件顺序表(control_order)
            AppControlOrder controlOrder = new AppControlOrder();
            BeanUtils.copyProperties(controlOrderReq, controlOrder);
            controlOrder.setAppId(appId);
            appControlOrderMapper.insert(controlOrder);
        }
        //保存文本控件配置
        if (formSaveReq.getTextControlConfigList().size() != 0) {
            for (AppTextControlConfigReq appTextControlConfigReq : formSaveReq.getTextControlConfigList()) {
                //数据校验
                if (isNullOrEmpty(appTextControlConfigReq.getControlUniqueId(), appTextControlConfigReq.getSubType(), appTextControlConfigReq.getName(),
                        appTextControlConfigReq.getRequired())) {
                    return CommonResult.failed(CommonCodeEnum.TEXT_CONTROL_NOT_SETTING_ENOUGH);
                }
                List<AppControlOrderReq> orderReqList = formSaveReq.getControlOrderList().stream().filter(controlOrder ->
                        controlOrder.getControlUniqueId().equals(appTextControlConfigReq.getControlUniqueId())).collect(Collectors.toList());
                AppControlOrder controlOrder = appControlOrderMapper.selectList(new LambdaQueryWrapper<AppControlOrder>()
                        .eq(AppControlOrder::getAppId, appId)
                        .eq(AppControlOrder::getSort, orderReqList.get(0).getSort())).get(0);

                AppTextControlConfig appTextControlConfig = new AppTextControlConfig();
                BeanUtils.copyProperties(appTextControlConfigReq, appTextControlConfig);
                appTextControlConfig.setAppId(appId);
                appTextControlConfig.setControlOrderId(controlOrder.getId());
                appTextControlConfigMapper.insert(appTextControlConfig);
            }
        }
        //保存时间控件配置
        if (formSaveReq.getTimeControlConfigList().size() != 0) {
            for (AppTimeControlConfigReq appTimeControlConfigReq : formSaveReq.getTimeControlConfigList()) {
                //数据校验
                if (isNullOrEmpty(appTimeControlConfigReq.getControlUniqueId(), appTimeControlConfigReq.getSubType(), appTimeControlConfigReq.getName(),
                        appTimeControlConfigReq.getRequired())) {
                    return CommonResult.failed(CommonCodeEnum.TIME_CONTROL_NOT_SETTING_ENOUGH);
                }
                //校验时间格式是否正确
                Date maxDate = TimeUtil.StringtoDate(appTimeControlConfigReq.getMaxDate());
                Date minDate = TimeUtil.StringtoDate(appTimeControlConfigReq.getMinDate());
                if (isNullOrEmpty(maxDate) || isNullOrEmpty(minDate)) {
                    return CommonResult.failed(CommonCodeEnum.TIME_CONTROL_FORMAT_ILLEGAL);
                }
                if (maxDate.compareTo(minDate) < 0) {
                    return CommonResult.failed(CommonCodeEnum.TIME_CONTROL_RANGE_ILLEGAL);
                }
                List<AppControlOrderReq> orderReqList = formSaveReq.getControlOrderList().stream().filter(controlOrder ->
                        controlOrder.getControlUniqueId().equals(appTimeControlConfigReq.getControlUniqueId())).collect(Collectors.toList());
                AppControlOrder controlOrder = appControlOrderMapper.selectList(new LambdaQueryWrapper<AppControlOrder>()
                        .eq(AppControlOrder::getAppId, appId)
                        .eq(AppControlOrder::getSort, orderReqList.get(0).getSort())).get(0);

                AppTimeControlConfig appTimeControlConfig = new AppTimeControlConfig();
                BeanUtils.copyProperties(appTimeControlConfigReq, appTimeControlConfig);
                appTimeControlConfig.setAppId(appId);
                appTimeControlConfig.setControlOrderId(controlOrder.getId());
                appTimeControlConfig.setMaxDate(TimeUtil.StringtoDate(appTimeControlConfigReq.getMaxDate()));
                appTimeControlConfig.setMinDate(TimeUtil.StringtoDate(appTimeControlConfigReq.getMinDate()));
                appTimeControlConfigMapper.insert(appTimeControlConfig);
            }
        }
        //保存文件控件配置
        if (formSaveReq.getFileControlConfigList().size() != 0) {
            for (AppFileControlConfigReq appFileControlConfigReq : formSaveReq.getFileControlConfigList()) {
                //数据校验
                if (isNullOrEmpty(appFileControlConfigReq.getControlUniqueId(), appFileControlConfigReq.getSubType(), appFileControlConfigReq.getName(),
                        appFileControlConfigReq.getRequired(), appFileControlConfigReq.getMaxCountLimit(), appFileControlConfigReq.getFileType(),
                        appFileControlConfigReq.getFileSize())) {
                    return CommonResult.failed(CommonCodeEnum.FILE_CONTROL_NOT_SETTING_ENOUGH);
                }
                List<AppControlOrderReq> orderReqList = formSaveReq.getControlOrderList().stream().filter(controlOrder ->
                        controlOrder.getControlUniqueId().equals(appFileControlConfigReq.getControlUniqueId())).collect(Collectors.toList());
                AppControlOrder controlOrder = appControlOrderMapper.selectList(new LambdaQueryWrapper<AppControlOrder>()
                        .eq(AppControlOrder::getAppId, appId)
                        .eq(AppControlOrder::getSort, orderReqList.get(0).getSort())).get(0);

                AppFileControlConfig appFileControlConfig = new AppFileControlConfig();
                BeanUtils.copyProperties(appFileControlConfigReq, appFileControlConfig);
                appFileControlConfig.setAppId(appId);
                appFileControlConfig.setControlOrderId(controlOrder.getId());
                appFileControlConfig.setFileType(JSON.toJSONString(appFileControlConfigReq.getFileType()));
                appFileControlConfigMapper.insert(appFileControlConfig);
            }
        }
        //保存选项控件配置
        if (formSaveReq.getOptionControlConfigList().size() != 0) {
            for (AppOptionControlConfigReq appOptionControlConfigReq : formSaveReq.getOptionControlConfigList()) {
                //数据校验
                if (isNullOrEmpty(appOptionControlConfigReq.getControlUniqueId(), appOptionControlConfigReq.getSubType(), appOptionControlConfigReq.getName(),
                        appOptionControlConfigReq.getRequired(), appOptionControlConfigReq.getHasDefaultValue())) {
                    return CommonResult.failed(CommonCodeEnum.OPTION_CONTROL_NOT_SETTING_ENOUGH);
                }
                if (appOptionControlConfigReq.getHasDefaultValue()) {
                    if (isNullOrEmpty(appOptionControlConfigReq.getDefaultValue())) {
                        return CommonResult.failed(CommonCodeEnum.OPTION_CONTROL_NOT_SETTING_ENOUGH);
                    }
                }
                List<AppControlOrderReq> orderReqList = formSaveReq.getControlOrderList().stream().filter(controlOrder ->
                        controlOrder.getControlUniqueId().equals(appOptionControlConfigReq.getControlUniqueId())).collect(Collectors.toList());
                AppControlOrder controlOrder = appControlOrderMapper.selectList(new LambdaQueryWrapper<AppControlOrder>()
                        .eq(AppControlOrder::getAppId, appId)
                        .eq(AppControlOrder::getSort, orderReqList.get(0).getSort())).get(0);

                AppOptionControlConfig appOptionControlConfig = new AppOptionControlConfig();
                BeanUtils.copyProperties(appOptionControlConfigReq, appOptionControlConfig);
                appOptionControlConfig.setAppId(appId);
                appOptionControlConfig.setControlOrderId(controlOrder.getId());
                appOptionControlConfig.setCheckboxList(JSON.toJSONString(appOptionControlConfigReq.getCheckboxList()));
                appOptionControlConfigMapper.insert(appOptionControlConfig);
            }
        }
        //保存步进器控件配置
        if (formSaveReq.getStepperControlConfigList().size() != 0) {
            for (AppStepperControlConfigReq appStepperControlConfigReq : formSaveReq.getStepperControlConfigList()) {
                //数据校验
                if (isNullOrEmpty(appStepperControlConfigReq.getControlUniqueId(), appStepperControlConfigReq.getSubType(),
                        appStepperControlConfigReq.getName(), appStepperControlConfigReq.getRequired(),
                        appStepperControlConfigReq.getStep(), appStepperControlConfigReq.getInputMax(), appStepperControlConfigReq.getInputMin(),
                        appStepperControlConfigReq.getIsInteger(), appStepperControlConfigReq.getIsInput(), appStepperControlConfigReq.getScale())) {
                    return CommonResult.failed(CommonCodeEnum.STEPPER_CONTROL_NOT_SETTING_ENOUGH);
                }
                List<AppControlOrderReq> orderReqList = formSaveReq.getControlOrderList().stream().filter(controlOrder ->
                        controlOrder.getControlUniqueId().equals(appStepperControlConfigReq.getControlUniqueId())).collect(Collectors.toList());
                AppControlOrder controlOrder = appControlOrderMapper.selectList(new LambdaQueryWrapper<AppControlOrder>()
                        .eq(AppControlOrder::getAppId, appId)
                        .eq(AppControlOrder::getSort, orderReqList.get(0).getSort())).get(0);

                AppStepperControlConfig appStepperControlConfig = new AppStepperControlConfig();
                BeanUtils.copyProperties(appStepperControlConfigReq, appStepperControlConfig);
                appStepperControlConfig.setAppId(appId);
                appStepperControlConfig.setControlOrderId(controlOrder.getId());
                appStepperControlConfigMapper.insert(appStepperControlConfig);
            }
        }

        return CommonResult.success();
    }

    @Override
    @Transactional
    public ResponseResult editFormDesign(FormDesignSaveReq formSaveReq) {
        //校验appId
        String appId = formSaveReq.getAppId();
        if (isNullOrEmpty(appId)) {
            return CommonResult.failed(CommonCodeEnum.DYNAMIC_APP_NOT_EXIST);
        }
        AppBasicInfo appBasicInfo = appBasicInfoMapper.selectById(appId);
        if (isNullOrEmpty(appBasicInfo) || appBasicInfo.getId() == null) {
            return CommonResult.failed(CommonCodeEnum.DYNAMIC_APP_NOT_EXIST);
        }
        if (formSaveReq.getControlOrderList().size() == 0) {
            return CommonResult.failed(CommonCodeEnum.CONTROL_NOT_ADD);
        }
        //校验控件是否完全设置
        if (formSaveReq.getControlOrderList().size() != formSaveReq.getTextControlConfigList().size() +
                formSaveReq.getTimeControlConfigList().size() + formSaveReq.getFileControlConfigList().size() +
                formSaveReq.getOptionControlConfigList().size() + formSaveReq.getStepperControlConfigList().size()) {
            return CommonResult.failed(CommonCodeEnum.CONTROL_NOT_SETTING_ENOUGH);
        }

        //如果当前应用有控件 先删除原有的控件
        List<AppControlOrder> appControlOrderList = appControlOrderMapper.selectList(new LambdaQueryWrapper<AppControlOrder>().eq(AppControlOrder::getAppId, appId));
        if (!appControlOrderList.isEmpty()) {
            //删除控件顺序表id
            appControlOrderMapper.delete(new LambdaQueryWrapper<AppControlOrder>().eq(AppControlOrder::getAppId, appId));
            //删除配置数据
            appTextControlConfigMapper.delete(new LambdaQueryWrapper<AppTextControlConfig>().eq(AppTextControlConfig::getAppId, appId));
            appTimeControlConfigMapper.delete(new LambdaQueryWrapper<AppTimeControlConfig>().eq(AppTimeControlConfig::getAppId, appId));
            appFileControlConfigMapper.delete(new LambdaQueryWrapper<AppFileControlConfig>().eq(AppFileControlConfig::getAppId, appId));
            appOptionControlConfigMapper.delete(new LambdaQueryWrapper<AppOptionControlConfig>().eq(AppOptionControlConfig::getAppId, appId));
            appStepperControlConfigMapper.delete(new LambdaQueryWrapper<AppStepperControlConfig>().eq(AppStepperControlConfig::getAppId, appId));
        }

        //保存数据
        for (AppControlOrderReq controlOrderReq : formSaveReq.getControlOrderList()) {
            if (isNullOrEmpty(controlOrderReq.getType(), controlOrderReq.getControlUniqueId(), controlOrderReq.getSort(), controlOrderReq.getIsShow())) {
                return CommonResult.failed(CommonCodeEnum.CONTROL_ORDER_NOT_SETTING_ENOUGH);
            }
            //保存控件顺序表(control_order)
            AppControlOrder controlOrder = new AppControlOrder();
            BeanUtils.copyProperties(controlOrderReq, controlOrder);
            controlOrder.setAppId(appId);
            appControlOrderMapper.insert(controlOrder);
        }
        //保存文本控件配置
        if (formSaveReq.getTextControlConfigList().size() != 0) {
            for (AppTextControlConfigReq appTextControlConfigReq : formSaveReq.getTextControlConfigList()) {
                //数据校验
                if (isNullOrEmpty(appTextControlConfigReq.getControlUniqueId(), appTextControlConfigReq.getSubType(), appTextControlConfigReq.getName(),
                        appTextControlConfigReq.getRequired(), appTextControlConfigReq.getNamePosition())) {
                    return CommonResult.failed(CommonCodeEnum.TEXT_CONTROL_NOT_SETTING_ENOUGH);
                }
                List<AppControlOrderReq> orderReqList = formSaveReq.getControlOrderList().stream().filter(controlOrder ->
                        controlOrder.getControlUniqueId().equals(appTextControlConfigReq.getControlUniqueId())).collect(Collectors.toList());
                AppControlOrder controlOrder = appControlOrderMapper.selectList(new LambdaQueryWrapper<AppControlOrder>()
                        .eq(AppControlOrder::getAppId, appId)
                        .eq(AppControlOrder::getSort, orderReqList.get(0).getSort())).get(0);

                AppTextControlConfig appTextControlConfig = new AppTextControlConfig();
                BeanUtils.copyProperties(appTextControlConfigReq, appTextControlConfig);
                appTextControlConfig.setAppId(appId);
                appTextControlConfig.setControlOrderId(controlOrder.getId());
                appTextControlConfigMapper.insert(appTextControlConfig);
            }
        }
        //保存时间控件配置
        if (formSaveReq.getTimeControlConfigList().size() != 0) {
            for (AppTimeControlConfigReq appTimeControlConfigReq : formSaveReq.getTimeControlConfigList()) {
                //数据校验
                if (isNullOrEmpty(appTimeControlConfigReq.getControlUniqueId(), appTimeControlConfigReq.getSubType(), appTimeControlConfigReq.getName(),
                        appTimeControlConfigReq.getRequired(), appTimeControlConfigReq.getNamePosition())) {
                    return CommonResult.failed(CommonCodeEnum.TIME_CONTROL_NOT_SETTING_ENOUGH);
                }
                //校验时间格式是否正确
                Date maxDate = TimeUtil.StringtoDate(appTimeControlConfigReq.getMaxDate());
                Date minDate = TimeUtil.StringtoDate(appTimeControlConfigReq.getMinDate());
                if (isNullOrEmpty(maxDate) || isNullOrEmpty(minDate)) {
                    return CommonResult.failed(CommonCodeEnum.TIME_CONTROL_FORMAT_ILLEGAL);
                }
                if (maxDate.compareTo(minDate) < 0) {
                    return CommonResult.failed(CommonCodeEnum.TIME_CONTROL_RANGE_ILLEGAL);
                }
                List<AppControlOrderReq> orderReqList = formSaveReq.getControlOrderList().stream().filter(controlOrder ->
                        controlOrder.getControlUniqueId().equals(appTimeControlConfigReq.getControlUniqueId())).collect(Collectors.toList());
                AppControlOrder controlOrder = appControlOrderMapper.selectList(new LambdaQueryWrapper<AppControlOrder>()
                        .eq(AppControlOrder::getAppId, appId)
                        .eq(AppControlOrder::getSort, orderReqList.get(0).getSort())).get(0);

                AppTimeControlConfig appTimeControlConfig = new AppTimeControlConfig();
                BeanUtils.copyProperties(appTimeControlConfigReq, appTimeControlConfig);
                appTimeControlConfig.setAppId(appId);
                appTimeControlConfig.setControlOrderId(controlOrder.getId());
                appTimeControlConfig.setMaxDate(TimeUtil.StringtoDate(appTimeControlConfigReq.getMaxDate()));
                appTimeControlConfig.setMinDate(TimeUtil.StringtoDate(appTimeControlConfigReq.getMinDate()));
                appTimeControlConfigMapper.insert(appTimeControlConfig);
            }
        }
        //保存文件控件配置
        if (formSaveReq.getFileControlConfigList().size() != 0) {
            for (AppFileControlConfigReq appFileControlConfigReq : formSaveReq.getFileControlConfigList()) {
                //数据校验
                if (isNullOrEmpty(appFileControlConfigReq.getControlUniqueId(), appFileControlConfigReq.getSubType(), appFileControlConfigReq.getName(),
                        appFileControlConfigReq.getRequired(), appFileControlConfigReq.getMaxCountLimit(), appFileControlConfigReq.getFileType(),
                        appFileControlConfigReq.getFileSize())) {
                    return CommonResult.failed(CommonCodeEnum.FILE_CONTROL_NOT_SETTING_ENOUGH);
                }
                List<AppControlOrderReq> orderReqList = formSaveReq.getControlOrderList().stream().filter(controlOrder ->
                        controlOrder.getControlUniqueId().equals(appFileControlConfigReq.getControlUniqueId())).collect(Collectors.toList());
                AppControlOrder controlOrder = appControlOrderMapper.selectList(new LambdaQueryWrapper<AppControlOrder>()
                        .eq(AppControlOrder::getAppId, appId)
                        .eq(AppControlOrder::getSort, orderReqList.get(0).getSort())).get(0);

                AppFileControlConfig appFileControlConfig = new AppFileControlConfig();
                BeanUtils.copyProperties(appFileControlConfigReq, appFileControlConfig);
                appFileControlConfig.setAppId(appId);
                appFileControlConfig.setControlOrderId(controlOrder.getId());
                appFileControlConfig.setFileType(JSON.toJSONString(appFileControlConfigReq.getFileType()));
                appFileControlConfigMapper.insert(appFileControlConfig);
            }
        }
        //保存选项控件配置
        if (formSaveReq.getOptionControlConfigList().size() != 0) {
            for (AppOptionControlConfigReq appOptionControlConfigReq : formSaveReq.getOptionControlConfigList()) {
                //数据校验
                if (isNullOrEmpty(appOptionControlConfigReq.getControlUniqueId(), appOptionControlConfigReq.getSubType(), appOptionControlConfigReq.getName(),
                        appOptionControlConfigReq.getRequired(), appOptionControlConfigReq.getHasDefaultValue(), appOptionControlConfigReq.getNamePosition())) {
                    return CommonResult.failed(CommonCodeEnum.OPTION_CONTROL_NOT_SETTING_ENOUGH);
                }
                if (appOptionControlConfigReq.getHasDefaultValue()) {
                    if (isNullOrEmpty(appOptionControlConfigReq.getDefaultValue())) {
                        return CommonResult.failed(CommonCodeEnum.OPTION_CONTROL_NOT_SETTING_ENOUGH);
                    }
                }
                List<AppControlOrderReq> orderReqList = formSaveReq.getControlOrderList().stream().filter(controlOrder ->
                        controlOrder.getControlUniqueId().equals(appOptionControlConfigReq.getControlUniqueId())).collect(Collectors.toList());
                AppControlOrder controlOrder = appControlOrderMapper.selectList(new LambdaQueryWrapper<AppControlOrder>()
                        .eq(AppControlOrder::getAppId, appId)
                        .eq(AppControlOrder::getSort, orderReqList.get(0).getSort())).get(0);

                AppOptionControlConfig appOptionControlConfig = new AppOptionControlConfig();
                BeanUtils.copyProperties(appOptionControlConfigReq, appOptionControlConfig);
                appOptionControlConfig.setAppId(appId);
                appOptionControlConfig.setControlOrderId(controlOrder.getId());
                appOptionControlConfig.setCheckboxList(JSON.toJSONString(appOptionControlConfigReq.getCheckboxList()));
                appOptionControlConfigMapper.insert(appOptionControlConfig);
            }
        }
        //保存步进器控件配置
        if (formSaveReq.getStepperControlConfigList().size() != 0) {
            for (AppStepperControlConfigReq appStepperControlConfigReq : formSaveReq.getStepperControlConfigList()) {
                //数据校验
                if (isNullOrEmpty(appStepperControlConfigReq.getControlUniqueId(), appStepperControlConfigReq.getSubType(),
                        appStepperControlConfigReq.getName(), appStepperControlConfigReq.getRequired(), appStepperControlConfigReq.getNamePosition(),
                        appStepperControlConfigReq.getStep(), appStepperControlConfigReq.getInputMax(), appStepperControlConfigReq.getInputMin(),
                        appStepperControlConfigReq.getIsInteger(), appStepperControlConfigReq.getIsInput(), appStepperControlConfigReq.getScale())) {
                    return CommonResult.failed(CommonCodeEnum.STEPPER_CONTROL_NOT_SETTING_ENOUGH);
                }
                List<AppControlOrderReq> orderReqList = formSaveReq.getControlOrderList().stream().filter(controlOrder ->
                        controlOrder.getControlUniqueId().equals(appStepperControlConfigReq.getControlUniqueId())).collect(Collectors.toList());
                AppControlOrder controlOrder = appControlOrderMapper.selectList(new LambdaQueryWrapper<AppControlOrder>()
                        .eq(AppControlOrder::getAppId, appId)
                        .eq(AppControlOrder::getSort, orderReqList.get(0).getSort())).get(0);

                AppStepperControlConfig appStepperControlConfig = new AppStepperControlConfig();
                BeanUtils.copyProperties(appStepperControlConfigReq, appStepperControlConfig);
                appStepperControlConfig.setAppId(appId);
                appStepperControlConfig.setControlOrderId(controlOrder.getId());
                appStepperControlConfigMapper.insert(appStepperControlConfig);
            }
        }

//        //修改节点权限
//        AppProcessNodeConfig appProcessNodeConfig = new AppProcessNodeConfig();
//        //设置权限
//        String authority = getAuthority(appId, true, true);
//        appProcessNodeConfig.setAuthority(authority);
//        appProcessNodeConfigMapper.update(appProcessNodeConfig, new LambdaQueryWrapper<AppProcessNodeConfig>()
//                .eq(AppProcessNodeConfig::getAppId, appId));

        //删除节点配置
        //删除条件
        List<String> conditionNodeIdList = appProcessNodeConfigMapper.selectList(new LambdaQueryWrapper<AppProcessNodeConfig>()
                        .eq(AppProcessNodeConfig::getAppId, appId)
                        .eq(AppProcessNodeConfig::getType, CONDITION_NODE))
                .stream().map(AppProcessNodeConfig::getId).collect(Collectors.toList());
        if (!conditionNodeIdList.isEmpty()) {
            appConditionGroupMapper.delete(new LambdaQueryWrapper<AppConditionGroup>().in(AppConditionGroup::getNodeId, conditionNodeIdList));
            appConditionMapper.delete(new LambdaQueryWrapper<AppCondition>().in(AppCondition::getNodeId, conditionNodeIdList));
        }
        //最后删除节点配置
        appProcessNodeConfigMapper.delete(new LambdaQueryWrapper<AppProcessNodeConfig>().eq(AppProcessNodeConfig::getAppId, appId));

        return CommonResult.success();
    }

    @Override
    public ResponseResult listFormDesign(String appId) {
        //校验appId
        if (isNullOrEmpty(appId)) {
            return CommonResult.failed(CommonCodeEnum.DYNAMIC_APP_NOT_EXIST);
        }
        AppBasicInfo appBasicInfo = appBasicInfoMapper.selectById(appId);
        if (isNullOrEmpty(appBasicInfo) || appBasicInfo.getId() == null) {
            return CommonResult.failed(CommonCodeEnum.DYNAMIC_APP_NOT_EXIST);
        }

        //包装数据
        FormDesignSaveResp saveResp = new FormDesignSaveResp();
        //包装控件顺序集合
        ArrayList<AppControlOrderResp> controlOrderRespArrayList = new ArrayList<>();
        List<AppControlOrder> appControlOrderList = appControlOrderMapper.selectList(new LambdaQueryWrapper<AppControlOrder>()
                .eq(AppControlOrder::getAppId, appId)
                .orderByAsc(AppControlOrder::getSort));
        for (AppControlOrder appControlOrder : appControlOrderList) {
            AppControlOrderResp appControlOrderResp = new AppControlOrderResp();
            BeanUtils.copyProperties(appControlOrder, appControlOrderResp);
            appControlOrderResp.setControlUniqueId(appControlOrder.getId());
            controlOrderRespArrayList.add(appControlOrderResp);
        }
        saveResp.setControlOrderList(controlOrderRespArrayList);
        //包装控件文本配置集合
        ArrayList<AppTextControlConfigResp> textControlConfigRespArrayList = new ArrayList<>();
        List<AppTextControlConfig> appTextControlConfigList = appTextControlConfigMapper.selectList(new LambdaQueryWrapper<AppTextControlConfig>().eq(AppTextControlConfig::getAppId, appId));
        if (!appTextControlConfigList.isEmpty()) {
            for (AppTextControlConfig appTextControlConfig : appTextControlConfigList) {
                AppTextControlConfigResp appTextControlConfigResp = new AppTextControlConfigResp();
                BeanUtils.copyProperties(appTextControlConfig, appTextControlConfigResp);
                appTextControlConfigResp.setControlUniqueId(appTextControlConfig.getControlOrderId());
                textControlConfigRespArrayList.add(appTextControlConfigResp);
            }
        }
        saveResp.setTextControlConfigList(textControlConfigRespArrayList);
        //包装控件时间配置集合
        ArrayList<AppTimeControlConfigResp> timeControlConfigRespArrayList = new ArrayList<>();
        List<AppTimeControlConfig> appTimeControlConfigList = appTimeControlConfigMapper.selectList(new LambdaQueryWrapper<AppTimeControlConfig>().eq(AppTimeControlConfig::getAppId, appId));
        if (!appTimeControlConfigList.isEmpty()) {
            for (AppTimeControlConfig appTimeControlConfig : appTimeControlConfigList) {
                AppTimeControlConfigResp appTimeControlConfigResp = new AppTimeControlConfigResp();
                BeanUtils.copyProperties(appTimeControlConfig, appTimeControlConfigResp);
                appTimeControlConfigResp.setControlUniqueId(appTimeControlConfig.getControlOrderId());
                timeControlConfigRespArrayList.add(appTimeControlConfigResp);
            }
        }
        saveResp.setTimeControlConfigList(timeControlConfigRespArrayList);
        //包装控件文件配置集合
        ArrayList<AppFileControlConfigResp> fileControlConfigRespArrayList = new ArrayList<>();
        List<AppFileControlConfig> appFileControlConfigList = appFileControlConfigMapper.selectList(new LambdaQueryWrapper<AppFileControlConfig>().eq(AppFileControlConfig::getAppId, appId));
        if (!appFileControlConfigList.isEmpty()) {
            for (AppFileControlConfig appFileControlConfig : appFileControlConfigList) {
                AppFileControlConfigResp appFileControlConfigResp = new AppFileControlConfigResp();
                BeanUtils.copyProperties(appFileControlConfig, appFileControlConfigResp);
                appFileControlConfigResp.setControlUniqueId(appFileControlConfig.getControlOrderId());
                if (!isNullOrEmpty(appFileControlConfig.getFileType())) {
                    appFileControlConfigResp.setFileType(JSON.parseArray(appFileControlConfig.getFileType(), String.class));
                }
                fileControlConfigRespArrayList.add(appFileControlConfigResp);
            }
        }
        saveResp.setFileControlConfigList(fileControlConfigRespArrayList);
        //包装控件选项配置集合
        ArrayList<AppOptionControlConfigResp> optionControlConfigRespArrayList = new ArrayList<>();
        List<AppOptionControlConfig> appOptionControlConfigList = appOptionControlConfigMapper.selectList(new LambdaQueryWrapper<AppOptionControlConfig>().eq(AppOptionControlConfig::getAppId, appId));
        if (!appOptionControlConfigList.isEmpty()) {
            for (AppOptionControlConfig appOptionControlConfig : appOptionControlConfigList) {
                AppOptionControlConfigResp appOptionControlConfigResp = new AppOptionControlConfigResp();
                BeanUtils.copyProperties(appOptionControlConfig, appOptionControlConfigResp);
                appOptionControlConfigResp.setControlUniqueId(appOptionControlConfig.getControlOrderId());
                if (!isNullOrEmpty(appOptionControlConfig.getCheckboxList())) {
                    List<CheckboxReq> checkboxReqList = JSON.parseArray(appOptionControlConfig.getCheckboxList(), CheckboxReq.class);
                    appOptionControlConfigResp.setCheckboxList(checkboxReqList);
                }
                optionControlConfigRespArrayList.add(appOptionControlConfigResp);
            }
        }
        saveResp.setOptionControlConfigList(optionControlConfigRespArrayList);
        //包装控件步进器配置集合
        ArrayList<AppStepperControlConfigResp> stepperControlConfigRespArrayList = new ArrayList<>();
        List<AppStepperControlConfig> appStepperControlConfigList = appStepperControlConfigMapper.selectList(new LambdaQueryWrapper<AppStepperControlConfig>().eq(AppStepperControlConfig::getAppId, appId));
        if (!appStepperControlConfigList.isEmpty()) {
            for (AppStepperControlConfig appStepperControlConfig : appStepperControlConfigList) {
                AppStepperControlConfigResp appStepperControlConfigResp = new AppStepperControlConfigResp();
                BeanUtils.copyProperties(appStepperControlConfig, appStepperControlConfigResp);
                appStepperControlConfigResp.setControlUniqueId(appStepperControlConfig.getControlOrderId());
                stepperControlConfigRespArrayList.add(appStepperControlConfigResp);
            }
        }
        saveResp.setStepperControlConfigList(stepperControlConfigRespArrayList);
        return CommonResult.success(saveResp);
    }

    /**
     * 获取节点权限
     *
     * @param appId
     * @return
     */
    public String getAuthority(String appId, Boolean readable, Boolean editable) {
        List<AuthorityResp> authorityRespList = new ArrayList<>();
        List<AppControlOrder> controlOrderList = appControlOrderMapper.selectList(new LambdaQueryWrapper<AppControlOrder>().eq(AppControlOrder::getAppId, appId));
        for (AppControlOrder controlOrder : controlOrderList) {
            if (Arrays.asList(TextControlType).contains(controlOrder.getType())) {
                AppTextControlConfig textControlConfig = appTextControlConfigMapper.selectList(new LambdaQueryWrapper<AppTextControlConfig>()
                        .eq(AppTextControlConfig::getControlOrderId, controlOrder.getId())).get(0);
                AuthorityResp authorityResp = new AuthorityResp();
                authorityResp.setName(textControlConfig.getName());
                authorityResp.setReadable(readable);
                authorityResp.setEditable(editable);
                authorityRespList.add(authorityResp);
            } else if (Arrays.asList(TimeControlType).contains(controlOrder.getType())) {
                AppTimeControlConfig timeControlConfig = appTimeControlConfigMapper.selectList(new LambdaQueryWrapper<AppTimeControlConfig>()
                        .eq(AppTimeControlConfig::getControlOrderId, controlOrder.getId())).get(0);
                AuthorityResp authorityResp = new AuthorityResp();
                authorityResp.setName(timeControlConfig.getName());
                authorityResp.setReadable(readable);
                authorityResp.setEditable(editable);
                authorityRespList.add(authorityResp);
            } else if (Arrays.asList(OptionControlType).contains(controlOrder.getType())) {
                AppOptionControlConfig optionControlConfig = appOptionControlConfigMapper.selectList(new LambdaQueryWrapper<AppOptionControlConfig>()
                        .eq(AppOptionControlConfig::getControlOrderId, controlOrder.getId())).get(0);
                AuthorityResp authorityResp = new AuthorityResp();
                authorityResp.setName(optionControlConfig.getName());
                authorityResp.setReadable(readable);
                authorityResp.setEditable(editable);
                authorityRespList.add(authorityResp);
            } else if (Arrays.asList(StepperControlType).contains(controlOrder.getType())) {
                AppStepperControlConfig stepperControlConfig = appStepperControlConfigMapper.selectList(new LambdaQueryWrapper<AppStepperControlConfig>()
                        .eq(AppStepperControlConfig::getControlOrderId, controlOrder.getId())).get(0);
                AuthorityResp authorityResp = new AuthorityResp();
                authorityResp.setName(stepperControlConfig.getName());
                authorityResp.setReadable(readable);
                authorityResp.setEditable(editable);
                authorityRespList.add(authorityResp);
            } else if (Arrays.asList(FileControlType).contains(controlOrder.getType())) {
                AppFileControlConfig fileControlConfig = appFileControlConfigMapper.selectList(new LambdaQueryWrapper<AppFileControlConfig>()
                        .eq(AppFileControlConfig::getControlOrderId, controlOrder.getId())).get(0);
                AuthorityResp authorityResp = new AuthorityResp();
                authorityResp.setName(fileControlConfig.getName());
                authorityResp.setReadable(readable);
                authorityResp.setEditable(editable);
                authorityRespList.add(authorityResp);
            }
        }
        return JSON.toJSONString(authorityRespList);
    }

}
